<?php

namespace App\Dao;

use App\Model\LineUpModel;
use App\Model\PriceOrdersModel;
use App\Services\SmsServices;
use Illuminate\Support\Facades\DB;

class LineUpPriceDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = LineUpModel::query();
        $query->when(checkIsHasField($params,'line_up_price_id'),function ($query)use ($params){
            $query->where('line_up_price_id',$params['line_up_price_id']);
        });
        $query->when(checkIsHasField($params,'shop_id'),function ($query)use ($params){
            $query->where('shop_id',$params['shop_id']);
        });
        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });
        $query->when(checkIsHasField($params,'status'),function ($query)use ($params){
            $query->where('status',$params['status']);
        });

        return $query->with('shopInfo')->with('userInfo');
    }

    public static function addCreate($price = 0,$user_id = 0,$shop_id = 0)
    {
        //添加排序
        return self::builder_query()->create([
            'money'=>$price,
            'now_price'=>0,
            'user_id'=>$user_id,
            'status'=>0,
            'shop_id'=>$shop_id
        ]);
    }

    public static function lines(array $all)
    {
        return self::builder_query($all)->get();
    }

    public static function lines_ranking($all)
    {
        $lists = self::builder_query($all)
            ->groupBy('user_id')
            ->selectRaw('sum(now_price) as priceses,user_id,shop_id')
            ->orderByDesc('priceses')
            ->get();
        return $lists;
    }

    /**
     * @desc 快捷支付
     * @param $out_trade_no
     * @return void
     */
    public static function dataPrices($out_trade_no)
    {
        $orderQUery = PriceOrdersModel::query()->where('order_code',$out_trade_no)->where('status',0);
        if(!$orderQUery->exists()){
            self::error('订单不存在');
        }
        $orderInfo = $orderQUery->first()->toArray();
        //金额
        DB::beginTransaction();

        try {

            $orderQUery->update(['status'=>1]);
            UsersDao::changePrices($orderInfo['price'],$orderInfo['user_id']);
            $dataP = $orderInfo['price'];
            $uer_id = $orderInfo['user_id'];
            $shop_id = $orderInfo['shop_id'];
            $rate = BusinessesDao::builder_query(['businesses_id'=>$orderInfo['shop_id']])->value('rate');
            $ratePrice = bcmul($dataP,(1-$rate),2);
            if($ratePrice > 0){
                LineUpPriceDao::addCreate($ratePrice,$uer_id,$shop_id);
            }
            $shopInfo = BusinessesDao::builder_query(['businesses_id'=>$orderInfo['shop_id']])->first()->toArray();
            $orderInfo['shop_info'] = $shopInfo;
            $rate_price_data = $orderInfo['shop_info']['rate_price_data'];
            $zongPrice = bcmul($orderInfo['price'],$rate_price_data,2);
            //参与返费
            if($zongPrice > 0){
                $rateRP = bcmul($zongPrice,0.1,2);
                SbillsDao::moneysUpdate($dataP,$shop_id,'店铺快捷收取费用到账');
                SbillsDao::moneysUpdate(-($zongPrice-$rateRP),$shop_id,'店铺扣除让利费用');
                SbillsDao::moneysUpdate(-($rateRP),$shop_id,'店铺扣除让利手续费手续费');
                //返费
                BusinessesDao::nowPrice(bcsub($zongPrice,$rateRP,2),$orderInfo['shop_info']['businesses_id'],$orderInfo['shop_info']);
            }


            SmsServices::sendNotice(($dataP-$zongPrice),$shopInfo['phone']);

            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error('失败'.$exception->getMessage().$exception->getLine());
        }
    }

    public static function addCrData(array $params)
    {
        $userQuery = UsersDao::builder_query([
            'phone'=>$params['phone']
        ]);
        if(!$userQuery->exists()){
            self::error('用户不存在!');
        }
        var_dump($params);
    }
}
